package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.ActivityDTO;
import io.github.talelin.latticy.mapper.ActivityMapper;
import io.github.talelin.latticy.mapper.CouponMapper;
import io.github.talelin.latticy.model.Activity;
import io.github.talelin.latticy.model.ActivityDetail;
import io.github.talelin.latticy.service.ActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tanrentao
 * @date 2021年08月10日 14:33
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper,Activity> implements ActivityService {

    @Autowired
    private CouponMapper couponMapper;

    public void create(ActivityDTO dto) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(dto, activity);
        this.save(activity);
    }

    public void update(ActivityDTO dto, Integer id) {
        Activity activity = this.getById(id);
        if (activity == null) {
            throw new NotFoundException(90000);
        }
        BeanUtils.copyProperties(dto, activity);
        this.updateById(activity);
    }

    public ActivityDetail getDetailById(Integer id) {
        Activity activity = this.getById(id);
        if (activity == null) {
            throw new NotFoundException(90000);
        }
        List<Integer> coupons = couponMapper.getCouponsByActivityId(id);
        ActivityDetail activityDetail = new ActivityDetail();
        activityDetail.setCouponIds(coupons);
        BeanUtils.copyProperties(activity, activityDetail);
        return activityDetail;
    }
}
